package com.codedef.diary.util;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.codedef.diary.domain.Diary;

import java.util.ArrayList;
import java.util.List;

public class DiaryUtil {

    public static List<Diary> getDiaryListByJsonArrayStr(String json) {
        List<Diary> diaryList = new ArrayList<>();
        JSONArray array = JSON.parseArray(json);
        for(int i=0; i<array.size(); i++) {
            JSONObject obj = (JSONObject) array.get(i);
            Diary diary = new Diary();
            diary.setId((Integer) obj.get("id"));
            diary.setUserId((Integer) obj.get("userId"));
            diary.setDateTime(DateUtil.dateConverter((String) obj.get("dateTime")));
            diary.setContent((String) obj.get("content"));
            diary.setWeather((String) obj.get("weather"));
            diary.setLocation((String) obj.get("location"));
            String[] pictureArray = ((String) obj.get("pictureList")).split("~");
            diary.setPictureList(pictureArray);
            diaryList.add(diary);
        }
        return diaryList;
    }

}
